package de.blanky.madMonkeys.map;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ViewFrame extends JPanel implements MouseListener {
	
	public static final int GRASS = 0;
	public static final int STONE = 1;
	public static final int BUSH = 2;
	
	private BufferedImage grass;
	private BufferedImage stone;
	private BufferedImage bush;
	
	private int width =8;
	private int height =8;
	private int[][]fields;
	private int currentSelectd;
	
	private String name;
	private Map saveMap;
	
	public int getCurrentSelectd() {
		return currentSelectd;
	}

	public void setCurrentSelectd(int currentSelectd) {
		this.currentSelectd = currentSelectd;
	}

	public ViewFrame(String n, int w, int h)
	{
		setPreferredSize(new Dimension(h*128, w*128));
		name = n;
		width = w;
		height = h;
		fields= new int[height][width];
		for(int i = 0; i<height;i++ ){
			for(int j = 0; j<width;j++){
				fields[i][j] = GRASS;
			}
		}
		
		try {
			grass = ImageIO.read(new File("src/de/blanky/madMonkeys/map/res/jungle_1.png"));
			stone = ImageIO.read(new File("src/de/blanky/madMonkeys/map/res/stone_1.png"));
			bush = ImageIO.read(new File("src/de/blanky/madMonkeys/map/res/jungle_2.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		addMouseListener(this);
	}
	
	public ViewFrame(ViewFrame vF)
	{
		setPreferredSize(new Dimension(vF.height*128, vF.width*128));
		name = vF.name;
		width = vF.width;
		height = vF.height;
		fields= new int[height][width];
		for(int i = 0; i<height;i++ ){
			for(int j = 0; j<width;j++){
				fields[i][j] = GRASS;
			}
		}
		
		try {
			grass = ImageIO.read(new File("src/de/blanky/madMonkeys/map/res/jungle_1.png"));
			stone = ImageIO.read(new File("src/de/blanky/madMonkeys/map/res/stone_1.png"));
			bush = ImageIO.read(new File("src/de/blanky/madMonkeys/map/res/jungle_2.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		addMouseListener(this);
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		for(int i = 0; i<height;i++ ){
			for(int j = 0; j<width;j++){
				if(fields[i][j] == GRASS){
					g.drawImage(grass, 128 * i, 128 * j, null);
				}
				
				if(fields[i][j] == STONE){
					g.drawImage(stone, 128 * i, 128 * j, null);

				}
				
				if(fields[i][j] == BUSH){
					g.drawImage(bush, 128 * i, 128 * j, null);

				}

				
			}
		}
		
				
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		int height = e.getY()/128;
		int width = e.getX()/128;
		
		try{
			if(e.getButton() == MouseEvent.BUTTON1){
				fields[width][height] = currentSelectd;
			}
			if(e.getButton() == MouseEvent.BUTTON3){
				fields[width][height] = GRASS;
			}
			
		} catch (Exception ee){
			System.out.println("ERROR, not in bounds");
		}
			
		
		
		System.out.println("clicked height: "+height+"; width: "+width+" set to " + currentSelectd);
		repaint();
		
		
		
	}
	public void saveMap(){
		try {
			ObjectOutputStream ou = new ObjectOutputStream(new FileOutputStream(this.getName()+".obj"));
			ou.writeObject(this);
			ou.flush();
			ou.close();
			ou.flush();
			System.out.println("Speichern erfolgreich!");
		} catch (FileNotFoundException e) {
			System.out.println("Dateifehler");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("IO-Fehler");
			e.printStackTrace();
		}
	}
	
	public void loadMap(String mapName){
		try{
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(mapName+".obj"));
			@SuppressWarnings("unchecked")
			ViewFrame vF = (ViewFrame) in.readObject();
			this.fields = vF.fields;
			this.name = vF.name;
			this.height = vF.height;
			this.width = vF.width;
			this.currentSelectd = vF.currentSelectd;
			
			in.close();
			System.out.println("Laden erfolgreich!");
		}catch (FileNotFoundException e) {
			System.err.println("Datei nicht gefunden!");
			
		}
		catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
